package ch.jamme.model.rel1;

import java.util.*;

import ch.jamme.model.*;

public class Order extends AbstractDomainObject{

	private Long orderNr;

	private Customer customer;

	private Shop shop;

	private OrderLine[] orderLines;

	private Map<OrderLine, Item> itemInLine = new HashMap<OrderLine, Item>();
	private Map<OrderLine, List<Item>> itemsInLine = new HashMap<OrderLine, List<Item>>();
	private List<Map<OrderLine, Item>> itemByLines = new ArrayList<Map<OrderLine, Item>>();
	
	public Date orderDate;

	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	public Long getOrderNr() {
		return orderNr;
	}

	public void setOrderNr(Long orderNr) {
		this.orderNr = orderNr;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public OrderLine[] getOrderLines() {
		return orderLines;
	}

	public void setOrderLines(OrderLine[] orderLines) {
		itemsInLine.clear();
		for (OrderLine orderLine : orderLines) {
			Item item = orderLine.getItem();
			if (item != null) {
				List<Item> items = new ArrayList<Item>();
				items.add(item);
				itemsInLine.put(orderLine, items);
				
				itemInLine.put(orderLine, item);
				
				Map<OrderLine, Item> itemByLine = new HashMap<OrderLine, Item>();
				itemByLine.put(orderLine, item);
				itemByLines.add(itemByLine);
			}
		}
		this.orderLines = orderLines;
	}

	public Date getOrderDate() {
		return orderDate;
	}
	
	public void setOrderDate(Date orderDate) {
		this.orderDate = orderDate;
	}
	
	public String getHumanReadableDescription() {
		return "Order no. "+getOrderNr();
	}

	public void addItem(ItemImpl item, int quantity){
		OrderLine orderLine = new OrderLine();
		orderLine.setItem(item);
		orderLine.setQuantity(quantity);
		orderLine.setLineNr(1);

		this.setOrderLines(new OrderLine[] { orderLine } );
	}

}
